'use strict';
app.controller('mobilePhoneWhiteCtrl',['$scope','$log','$modal','utilService', function($scope,$log,$modal,utilService){
    // call server.
    //查询手机白名单
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'system/user/white/list/phone',
        }).then(function(result){
        });
    }
    $scope.query();

    //添加手机白名单
    $scope.openWhiteAdd = function (size) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/systemSetting/mobile_phone_white_add.html',
            controller: 'addMobilePhoneWhiteCtrl',
            scope:$scope,
            size: size,
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };

    //删除白名单
    $scope.openDelete = function (phone) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/systemSetting/mobile_phone_white_delete.html',
            controller: 'delMobilePhoneWhiteCtrl',
            scope: $scope,
            size:'sm',
            resolve: {
                data: function () {
                    var data = {};
                    data.phone = phone;
                    return data;
                }
            }
        });
    }
}]);

    //新增白名单模态框Controller
app.controller('addMobilePhoneWhiteCtrl', ['$scope', '$modalInstance','$state','mobilePhoneWhiteService','toaster',
    function($scope, $modalInstance,$state,mobilePhoneWhiteService,toaster) {

        $scope.AllowToAdd=true;

        $scope.addMobilePhoneWhite = function(){
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;
            mobilePhoneWhiteService.addMobilePhoneWhite($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToAdd=true;
                        $scope.query()
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToAdd=true;
                        break
                }
            });
        };

        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };

    }]);

    //删除模态框Controller
app.controller('delMobilePhoneWhiteCtrl', ['$scope', '$modalInstance','$state','mobilePhoneWhiteService','toaster','data',
    function($scope, $modalInstance,$state,mobilePhoneWhiteService,toaster,data) {
        $scope.phone=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            mobilePhoneWhiteService.delMobilePhoneWhite($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.AllowToDelete=true;
                        $scope.query()
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);


